package directory

import (
	"context"

	"mall/service/attachment/rpc/attachmentclient"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/api/internal/utils"
	"mall/service/backend/common/i18n"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetDirectoryByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetDirectoryByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDirectoryByIdLogic {
	return &GetDirectoryByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetDirectoryByIdLogic) GetDirectoryById(req *types.UUIDReq) (resp *types.MediaFileInfoResp, err error) {
	data, err := l.svcCtx.AttachmentRpc.BackendGetFileById(l.ctx, &attachmentclient.FileIdReq{
		Id: req.Id,
	})
	if err != nil {
		return nil, err
	}

	option := utils.GetOptionByStatus(data.Status)

	return &types.MediaFileInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: &types.MediaFileInfo{
			BaseUUIDInfo: types.BaseUUIDInfo{
				Id:         data.Id,
				CreatedAt:  data.CreatedAt,
				UpdatedAt:  data.UpdatedAt,
				DeletedAt:  data.DeletedAt,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			UserId:   data.UserId,
			ParentId: data.ParentId,
			Title:    data.AltText,
			Excerpt:  data.Excerpt,
			FileType: data.Type,
			Status:   data.Status,
			Sort:     data.Sort,
			Path:     data.Filename,
			FullPath: data.Path,
		},
	}, nil
}
